﻿using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using Microsoft.AspNet.Identity;
using NPOI.HSSF.UserModel;
using OfficeOpenXml;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Globalization;
using System.Drawing;

namespace GPS.Controllers.GPS_DanhMuc
{
    public class GPS_GiaoVienController : FDBController
    {
        #region unit
        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
        public GPS.MODELS.ApplicationUser getCurrentUser()
        {
            ApplicationDbContext dbUser = new ApplicationDbContext();
            string userId = User.Identity.GetUserId();
            ApplicationUser _currentUser = dbUser.Users.Find(userId);
            return _currentUser;
        }
        public void initialCategorySearchAction(ViewModelQLGV viewModel)
        {
            ViewBag.GV_HANGDT_MAs = new SelectList(_db.Filter<SHANGDT>(x => x.HANGDT_RANK >= 5), "HANGDT_MA", "HANGDT_TEN");
            ViewBag.checkuser = this.getCurrentUser().MA_TINHTP;
            string getdvgtvt_Ma = GPS.DAL.Check_User.CheckUser(this.getCurrentUser().MA_TINHTP, viewModel.pGV_DVGTVT_MA);
            viewModel.pGV_DVGTVT_MA = getdvgtvt_Ma;

            string hodem = null;
            string ten = null;
            GPS.DAL.Check_User.Substring_hoten(viewModel.pGV_TEN, ref hodem, ref ten);
            viewModel.pGV_HODEM = hodem;
            viewModel.pGV_TEN = ten;

            viewModel.pROW = viewModel.pROW == null ? "50" : viewModel.pROW;
            viewModel.pGV_HTDTAO_MA = viewModel.ExportButton == null ? null : "";
        }
        public void initialCategoryWordAction(CrudModelQLGV _model)
        {
            _model.GV_HTDTAO_MA = _model.GV_HTDTAO_MA == null ? "" : _db.GetOne<SHTDAOTAO>(x => x.HTDT_MA == _model.GV_HTDTAO_MA).HTDT_MOTA;
            _model.GV_HTTUYENDUNG = _model.GV_HTTUYENDUNG == "HĐ" ? "Hợp đồng" : "Chính thức";
        }
        #endregion

        #region index
        public ActionResult Index(ViewModelQLGV SearchModel, int? currentPage)
        {
            initialCategorySearchAction(SearchModel);
            List<CrudModelQLGV> model = new List<CrudModelQLGV>();
            if (!string.IsNullOrEmpty(SearchModel.ExportButton))
            {
                if (!string.IsNullOrEmpty(SearchModel.pGV_DVGTVT_MA))
                {
                    model = GetListData(SearchModel);
                }
                ViewBag.TotalRow = model == null ? "0" : model.Count().ToString();
                string tempAdd = "";
                string FileName = "";
                List<CrudModelQLGV> SubModel = new List<CrudModelQLGV>();
                if (SearchModel.ExportButton == "Danh sách giáo viên")
                {
                    tempAdd = "DanhSachGiaoVien.xlsx";
                    FileName = "DanhSachGiaoVien_" + DateTime.Now.ToShortDateString() + ".xlsx";
                }
                else if (SearchModel.ExportButton == "Danh sách giáo viên thực hành")
                {
                    SearchModel.pGV_HTDTAO_MA = "TH";
                    tempAdd = "DanhSachGiaoVien_ThucHanh.xlsx";
                    FileName = "DanhSachGiaoVienThucHanh_" + DateTime.Now.ToShortDateString() + ".xlsx";
                }
                else if (SearchModel.ExportButton == "Danh sách giáo viên lý thuyết")
                {
                    SearchModel.pGV_HTDTAO_MA = "LT";
                    tempAdd = "DanhSachGiaoVien_LyThuyet.xlsx";
                    FileName = "DanhSachGiaoVienLyThuyet_" + DateTime.Now.ToShortDateString() + ".xlsx";
                }
                else if (SearchModel.ExportButton == "Tìm kiếm")
                {
                    var pageIndex = SearchModel.Page ?? 1;
                    SearchModel.SearchResults = model.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);
                    return View(SearchModel);
                }

                try
                {
                    SubModel = GetListData(SearchModel);
                    FileInfo template = new FileInfo(Server.MapPath(string.Format(@"\Templates\GPS_Excel\{0}", tempAdd)));
                    FileInfo newFile = new FileInfo(Server.MapPath(@"\Templates\GPS_Excel\CB_DonViHetHanGiayPhepNEW.xlsx"));
                    // Using the template to create the newFile...
                    using (OfficeOpenXml.ExcelPackage excelPackage = new OfficeOpenXml.ExcelPackage(newFile, template))
                    {
                        // Getting the complete workbook...
                        ExcelWorkbook myWorkbook = excelPackage.Workbook;
                        // Getting the worksheet by its name...
                        ExcelWorksheet myWorksheet = myWorkbook.Worksheets["Sheet1"];
                        int recordIndex = 8;
                        foreach (var item in SubModel)
                        {
                            myWorksheet.Cells[recordIndex, 1].Value = item.STT.ToString();
                            myWorksheet.Cells[recordIndex, 2].Value = item.GV_HOTEN;
                            myWorksheet.Cells[recordIndex, 3].Value = item.GV_NGAYSINH == null ? "" : item.GV_NGAYSINH;
                            myWorksheet.Cells[recordIndex, 4].Value = item.GV_SOCMT == null ? "" : item.GV_SOCMT;
                            myWorksheet.Cells[recordIndex, 5].Value = item.GV_NOICT == null ? "" : item.GV_NOICT;
                            myWorksheet.Cells[recordIndex, 6].Value = item.GV_HTTUYENDUNG == "CT" ? "X" : "";
                            myWorksheet.Cells[recordIndex, 7].Value = item.GV_HTTUYENDUNG == "HD" ? "X" : "";
                            myWorksheet.Cells[recordIndex, 8].Value = item.GV_TDVANHOA == null ? "" : item.GV_TDVANHOA.ToString();
                            myWorksheet.Cells[recordIndex, 9].Value = item.GV_TDCHUYENMON == null ? "" : item.GV_TDCHUYENMON.ToString();
                            myWorksheet.Cells[recordIndex, 10].Value = item.GV_TDSUPHAM == null ? "" : item.GV_TDSUPHAM.ToString();
                            myWorksheet.Cells[recordIndex, 11].Value = item.GV_HGPLX_MA == null ? "" : item.GV_HGPLX_MA;
                            myWorksheet.Cells[recordIndex, 12].Value = item.GV_NGAYCAPGPLX == null ? "" : item.GV_NGAYCAPGPLX;
                            myWorksheet.Cells[recordIndex, 13].Value = item.GV_THAMNIENLX == null ? "" : item.GV_THAMNIENLX.ToString();
                            myWorksheet.Cells[recordIndex, 14].Value = item.GV_MOTA == null ? "" : item.GV_MOTA.ToString();
                            recordIndex++;
                        }
                        if (SearchModel.pGV_DVGTVT_MA != null)
                        {
                            myWorksheet.Cells[2, 1].Value = "Cơ sở giao thông vận tải" + _db.GetOne<SDONVIGTVT>(x => x.DVGTVT_MA == SearchModel.pGV_DVGTVT_MA).DVGTVT_TEN; ;
                        }
                        if (SearchModel.pGV_HANGDT_MA != null)
                        {
                            myWorksheet.Cells[3, 1].Value = "Hạng đào tạo: " + _db.GetOne<SHANGDT>(x => x.HANGDT_MA == SearchModel.pGV_HANGDT_MA).HANGDT_TEN; ;
                        }
                        excelPackage.Save();
                    }
                    string contentType = string.Empty;
                    string CurrentFileName = Server.MapPath(@"\Templates\GPS_Excel\CB_DonViHetHanGiayPhepNEW.xlsx");
                    if (CurrentFileName.Contains(".xlsx"))
                    {
                        contentType = "application/msexcel";
                    }
                    return File(CurrentFileName, contentType, FileName);
                }
                catch (Exception ex)
                {
                    TempData["Message"] = ex.ToString();
                    return RedirectToAction("Index");
                }
            }
            else
            {
                ViewBag.TotalRow = model == null ? "0" : model.Count().ToString();
                var pageIndex = SearchModel.Page ?? 1;
                SearchModel.SearchResults = model.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);
                return View(SearchModel);
            }
        }
        #endregion

        #region Detail
        public ActionResult Detail(string id, string id1)
        {
            string _GV_MA = id;
            string _GV_DVGTVT_MA = id1;
            CrudModelQLGV _model = GetItemData(_GV_MA, _GV_DVGTVT_MA);
            return PartialView("Detail", _model);
        }
        #endregion

        #region"Ajax"
        public JsonResult AutoCompleteGV_NOICT_DVHC_MA(string term)
        {
            var result = _db.Filter<SDONVIHC>(o => o.DVHC_MA.Contains(term) || o.DVHC_TENDAYDU.ToUpper().Contains(term.ToUpper()));
            return Json(result, JsonRequestBehavior.AllowGet);
        }
        public JsonResult AutoCompleteGV_DVGTVT_MA(string term)
        {
            var result = _db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40" && (o.DVGTVT_MA.Contains(term) || o.DVGTVT_TEN.ToUpper().Contains(term.ToUpper()))
                                                                                && o.DVGTVT_ACTIVE == "1");
            return Json(result, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public ActionResult getGV_MA(string gv_DVGTVT_MA)
        {
            var maxGV_MA = _db.Filter<DGV>(o => o.GV_DVGTVT_MA == gv_DVGTVT_MA).OrderByDescending(o => o.GV_MA).FirstOrDefault();
            string strGV_MA = gv_DVGTVT_MA + "001";

            if (maxGV_MA != null)
            {
                var curGV_MA = int.Parse(maxGV_MA.GV_MA) + 1;
                strGV_MA = curGV_MA.ToString();
            }
            return Content(string.Join("", strGV_MA));
        }
        #endregion

        #region ExportWord
        public ActionResult ExportWord(string id, string id1)
        {
            string _GV_MA = id;
            string _GV_DVGTVT_MA = id1;
            CrudModelQLGV _model = GetItemData(_GV_MA, _GV_DVGTVT_MA);
            initialCategoryWordAction(_model);
            byte[] contentFile = null;
            contentFile = GPS.ENTITYS.EXPORTWORD_QLGV.EXPORT_QLGV(Server.MapPath("~/Templates/GPS_Word/Giaovien_soyeulylich.docx"), _model, "DocxGenericReport");
            string nameOutput = "LyLichGiaoVien_" + _model.GV_HOTEN + ".docx";
            return File(contentFile, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", nameOutput);
        }
        #endregion

        #region Getdatabinding
        public List<CrudModelQLGV> GetListData(ViewModelQLGV SearchModel)
        {
            var _pGV_MA = new OracleParameter("pGV_MA", OracleDbType.Varchar2, (SearchModel.pGV_MA == null ? DBNull.Value : (object)SearchModel.pGV_MA), ParameterDirection.Input);
            var _pGV_HODEM = new OracleParameter("pGV_HODEM", OracleDbType.Varchar2, (SearchModel.pGV_HODEM == null ? DBNull.Value : (object)SearchModel.pGV_HODEM.Trim()), ParameterDirection.Input);
            var _pGV_TEN = new OracleParameter("pGV_TEN", OracleDbType.Varchar2, (SearchModel.pGV_TEN == null ? DBNull.Value : (object)SearchModel.pGV_TEN.Trim()), ParameterDirection.Input);
            var _pGV_DVGTVT_MA = new OracleParameter("pGV_DVGTVT_MA", OracleDbType.Varchar2, (SearchModel.pGV_DVGTVT_MA == null ? DBNull.Value : (object)SearchModel.pGV_DVGTVT_MA), ParameterDirection.Input);
            var _pGV_HANGDT_MA = new OracleParameter("pGV_HANGDT_MA", OracleDbType.Varchar2, (SearchModel.pGV_HANGDT_MA == null ? DBNull.Value : (object)SearchModel.pGV_HANGDT_MA), ParameterDirection.Input);
            var _pGV_SOCMT = new OracleParameter("pGV_SOCMT", OracleDbType.Varchar2, (SearchModel.pGV_SOCMT == null ? DBNull.Value : (object)SearchModel.pGV_SOCMT), ParameterDirection.Input);
            var _pGV_HTDTAO_MA = new OracleParameter("pGV_HTDTAO_MA", OracleDbType.Varchar2, (SearchModel.pGV_HTDTAO_MA == null ? DBNull.Value : (object)SearchModel.pGV_HTDTAO_MA), ParameterDirection.Input);
            var _pGV_TEL = new OracleParameter("pGV_TEL", OracleDbType.Varchar2, (SearchModel.pGV_TEL == null ? DBNull.Value : (object)SearchModel.pGV_TEL), ParameterDirection.Input);
            var _pGV_TDCHUYENMON = new OracleParameter("pGV_TDCHUYENMON", OracleDbType.Varchar2, (SearchModel.pGV_TDCHUYENMON == null ? DBNull.Value : (object)SearchModel.pGV_TDCHUYENMON), ParameterDirection.Input);
            var _pROW = new OracleParameter("pROW", OracleDbType.Decimal, (SearchModel.pROW == null ? DBNull.Value : (object)SearchModel.pROW), ParameterDirection.Input);
            var _pRET = new OracleParameter("pRET", OracleDbType.RefCursor, ParameterDirection.Output);
            var entities = _db.DbContext.Database.SqlQuery<CrudModelQLGV>("BEGIN GPS_HOCVIEN.GPS_GETDS_SEARCH_GIAOVIEN(:pGV_MA,:pGV_HODEM,:pGV_TEN,:pGV_DVGTVT_MA, :pGV_HANGDT_MA,:pGV_SOCMT, :pGV_HTDTAO_MA, :pGV_TEL,:pGV_TDCHUYENMON,:pROW,:pRET); end;"
                                                        , _pGV_MA
                                                        , _pGV_HODEM
                                                        , _pGV_TEN
                                                        , _pGV_DVGTVT_MA
                                                        , _pGV_HANGDT_MA
                                                        , _pGV_SOCMT
                                                        , _pGV_HTDTAO_MA
                                                        , _pGV_TEL
                                                        , _pGV_TDCHUYENMON
                                                        , _pROW
                                                        , _pRET).ToList();
            return entities;
        }
        public CrudModelQLGV GetItemData(string GV_MA, string GV_DVGTVT_MA)
        {
            var _pGV_MA = new OracleParameter("pGV_MA", OracleDbType.Varchar2, (GV_MA == null ? DBNull.Value : (object)GV_MA), ParameterDirection.Input);
            var _pGV_HODEM = new OracleParameter("pGV_HODEM", OracleDbType.Varchar2, DBNull.Value, ParameterDirection.Input);
            var _pGV_TEN = new OracleParameter("pGV_TEN", OracleDbType.Varchar2, DBNull.Value, ParameterDirection.Input);
            var _pGV_DVGTVT_MA = new OracleParameter("pGV_DVGTVT_MA", OracleDbType.Varchar2, (GV_DVGTVT_MA == null ? DBNull.Value : (object)GV_DVGTVT_MA), ParameterDirection.Input);
            var _pGV_HANGDT_MA = new OracleParameter("pGV_HANGDT_MA", OracleDbType.Varchar2, DBNull.Value, ParameterDirection.Input);
            var _pGV_SOCMT = new OracleParameter("pGV_SOCMT", OracleDbType.Varchar2, DBNull.Value, ParameterDirection.Input);
            var _pGV_HTDTAO_MA = new OracleParameter("pGV_HTDTAO_MA", OracleDbType.Varchar2, DBNull.Value, ParameterDirection.Input);
            var _pGV_TEL = new OracleParameter("pGV_TEL", OracleDbType.Varchar2, DBNull.Value, ParameterDirection.Input);
            var _pGV_TDCHUYENMON = new OracleParameter("pGV_TDCHUYENMON", OracleDbType.Varchar2, DBNull.Value, ParameterDirection.Input);
            var _pROW = new OracleParameter("pROW", OracleDbType.Decimal, "1", ParameterDirection.Input);
            var _pRET = new OracleParameter("pRET", OracleDbType.RefCursor, ParameterDirection.Output);
            var entities = _db.DbContext.Database.SqlQuery<CrudModelQLGV>("BEGIN GPS_HOCVIEN.GPS_GETDS_SEARCH_GIAOVIEN(:pGV_MA,:pGV_HODEM,:pGV_TEN,:pGV_DVGTVT_MA, :pGV_HANGDT_MA,:pGV_SOCMT, :pGV_HTDTAO_MA, :pGV_TEL,:pGV_TDCHUYENMON,:pROW,:pRET); end;"
                                                        , _pGV_MA
                                                        , _pGV_HODEM
                                                        , _pGV_TEN
                                                        , _pGV_DVGTVT_MA
                                                        , _pGV_HANGDT_MA
                                                        , _pGV_SOCMT
                                                        , _pGV_HTDTAO_MA
                                                        , _pGV_TEL
                                                        , _pGV_TDCHUYENMON
                                                        , _pROW
                                                        , _pRET).FirstOrDefault();
            return entities;
        }
        #endregion

        #region Disable
        #region Create
        //public ActionResult Create()
        //{
        //    initialCategoryCreateAction();
        //    return View(new CrudModelDGV());
        //}

        //[HttpPost]
        //public ActionResult Create(CrudModelDGV model)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        Int16 iRank = 0;
        //        if (!string.IsNullOrEmpty(model.GV_HANGDT_MA))
        //        {
        //            string strRank = _db.Filter<SHANGDT>(x => x.HANGDT_MA == model.GV_HANGDT_MA).Select(i => i.HANGDT_RANK).FirstOrDefault().ToString();
        //            iRank = Convert.ToInt16(strRank);
        //        }
        //        DGV _model = new DGV
        //        {
        //            ID = EFSequence.GetNextValue("SEQ_DGV_ID"),
        //            GV_MA = GetkeyIdentity_GV_MA(model.GV_DVGTVT_MA.Split(':')[0]),
        //            GV_DVGTVT_MA = model.GV_DVGTVT_MA.Split(':')[0],
        //            GV_HANGDT_MA = model.GV_HANGDT_MA,
        //            GV_HODEM = model.GV_HODEM,
        //            GV_TEN = model.GV_TEN,
        //            GV_NGAYSINH = model.GV_NGAYSINH,
        //            GV_HANGDT_RANK = iRank,
        //            GV_SOCMT = model.GV_SOCMT,
        //            GV_NOICT = model.GV_NOICT,

        //            GV_NOICT_DVHC_MA = (!string.IsNullOrWhiteSpace(model.GV_NOICT_DVHC_MA) ? model.GV_NOICT_DVHC_MA.Split(':')[0] : string.Empty),
        //            GV_NOICT_DVGTVT_MA = (!string.IsNullOrWhiteSpace(model.GV_NOICT_DVGTVT_MA) ? model.GV_NOICT_DVGTVT_MA.Split(':')[0] : string.Empty),

        //            GV_GIOITINH = model.GV_GIOITINH,
        //            GV_TEL = model.GV_TEL,
        //            GV_HTTUYENDUNG = model.GV_HTTUYENDUNG,
        //            GV_TDVANHOA = model.GV_TDVANHOA,
        //            GV_TDCHUYENMON = model.GV_TDCHUYENMON,
        //            GV_TDSUPHAM = model.GV_TDSUPHAM,
        //            GV_HGPLX_MA = model.GV_HGPLX_MA,
        //            GV_NGAYCAPGPLX = model.GV_NGAYCAPGPLX,
        //            GV_THAMNIENLX = model.GV_THAMNIENLX,
        //            GV_SOQD_GCN = model.GV_SOQD_GCN,
        //            GV_NGAYQD_GCN = model.GV_NGAYQD_GCN,
        //            GV_HTDTAO_MA = model.GV_HTDTAO_MA,
        //            GV_CAUTAOSUACHUA = (model.GV_CAUTAOSUACHUA == false ? "0" : "1"),
        //            GV_DAODUCLAIXE = (model.GV_DAODUCLAIXE == false ? "0" : "1"),
        //            GV_NGHIEPVUVANTAI = (model.GV_NGHIEPVUVANTAI == false ? "0" : "1"),
        //            GV_LUATGTDB = (model.GV_LUATGTDB == false ? "0" : "1"),
        //            GV_KYTHUATLAIXE = (model.GV_KYTHUATLAIXE == false ? "0" : "1"),
        //            GV_MOTA = model.GV_MOTA,
        //            GV_ACTIVE = (model.GV_ACTIVE == false ? "0" : "1"),
        //            GV_NGUOINHAP = User.Identity.Name,
        //            GV_NGAYNHAP = DateTime.Now
        //        };

        //        if (model.vGV_ANHCD != null)
        //        {
        //            HttpPostedFileBase file = model.vGV_ANHCD;
        //            using (var binaryReader = new BinaryReader(file.InputStream))
        //            {
        //                _model.GV_ANHCD = binaryReader.ReadBytes(file.ContentLength);
        //            }
        //        }
        //        _db.Insert<DGV>(_model);
        //        return RedirectToAction("Index");
        //    }
        //    return View(model);
        //}
        //public void initialCategoryCreateAction()
        //{
        //    // ApplicationUser curUser = getCurrentUser();

        //    ViewBag.GV_DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");
        //    ViewBag.GV_HANGDT_MAs = new SelectList(_db.GetAll<SHANGDT>(), "HANGDT_MA", "HANGDT_TEN");
        //    ViewBag.GV_HGPLX_MAs = new SelectList(_db.GetAll<SHANGGPLX>(), "HGPLX_MA", "HGPLX_TEN");
        //    ViewBag.GV_NOICT_DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");
        //    ViewBag.GV_NOICT_DVHC_MAs = new SelectList(_db.GetAll<SDONVIHC>(), "DVHC_MA", "DVHC_TENDAYDU");
        //    ViewBag.GV_HTDTAO_MAs = new SelectList(_db.Filter<SHTDAOTAO>(o => o.HTDT_ACTIVE == "1"), "HTDT_MA", "HTDT_MOTA");
        //    ViewBag.GV_HTTUYENDUNGs = "CT.HĐ";
        //}
        #endregion
        #region Edit
        //public ActionResult Edit(decimal id)
        //{

        //    DGV model = _db.GetOne<DGV>(o => o.ID == id);
        //    CrudModelDGV _model = CopyObject_model(model);
        //    var dvgtvt = _db.GetOne<SDONVIGTVT>(o => o.DVGTVT_MA == model.GV_DVGTVT_MA);
        //    _model.GV_DVGTVT_MA = dvgtvt.DVGTVT_MA + ":" + dvgtvt.DVGTVT_TEN;
        //    if (!string.IsNullOrWhiteSpace(model.GV_NOICT_DVHC_MA))
        //    {
        //        var dvghc = _db.GetOne<SDONVIHC>(o => o.DVHC_MA == model.GV_NOICT_DVHC_MA);
        //        _model.GV_NOICT_DVHC_MA = dvghc.DVHC_MA + ":" + dvghc.DVHC_TENDAYDU;
        //    }

        //    if (!string.IsNullOrWhiteSpace(model.GV_NOICT_DVGTVT_MA))
        //    {
        //        var dvghcgtvt = _db.GetOne<SDONVIGTVT>(o => o.DVGTVT_MA == model.GV_NOICT_DVGTVT_MA);
        //        _model.GV_NOICT_DVGTVT_MA = dvghcgtvt.DVGTVT_MA + ":" + dvghcgtvt.DVGTVT_TEN;
        //    }

        //    initialCategoryEditAction(_model);
        //    return View(_model);
        //}
        //[HttpPost]
        //public ActionResult Edit(CrudModelDGV model)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        var _model = _db.GetOne<DGV>(o => o.ID == model.ID);
        //        _model.GV_DVGTVT_MA = model.GV_DVGTVT_MA.Split(':')[0];
        //        _model.GV_HANGDT_MA = model.GV_HANGDT_MA;
        //        _model.GV_HODEM = model.GV_HODEM;
        //        _model.GV_TEN = model.GV_TEN;
        //        _model.GV_NGAYSINH = model.GV_NGAYSINH;
        //        _model.GV_SOCMT = model.GV_SOCMT;
        //        _model.GV_NOICT = model.GV_NOICT;
        //        _model.GV_NOICT_DVHC_MA = (!string.IsNullOrWhiteSpace(model.GV_NOICT_DVHC_MA) ? model.GV_NOICT_DVHC_MA.Split(':')[0] : string.Empty);
        //        _model.GV_NOICT_DVGTVT_MA = (!string.IsNullOrWhiteSpace(model.GV_NOICT_DVGTVT_MA) ? model.GV_NOICT_DVGTVT_MA.Split(':')[0] : string.Empty);
        //        _model.GV_GIOITINH = model.GV_GIOITINH;
        //        _model.GV_TEL = model.GV_TEL;
        //        _model.GV_HTTUYENDUNG = model.GV_HTTUYENDUNG;
        //        _model.GV_TDVANHOA = model.GV_TDVANHOA;
        //        _model.GV_TDCHUYENMON = model.GV_TDCHUYENMON;
        //        _model.GV_TDSUPHAM = model.GV_TDSUPHAM;
        //        _model.GV_HGPLX_MA = model.GV_HGPLX_MA;
        //        _model.GV_NGAYCAPGPLX = model.GV_NGAYCAPGPLX;
        //        _model.GV_THAMNIENLX = model.GV_THAMNIENLX;
        //        _model.GV_SOQD_GCN = model.GV_SOQD_GCN;
        //        _model.GV_NGAYQD_GCN = model.GV_NGAYQD_GCN;
        //        _model.GV_HTDTAO_MA = model.GV_HTDTAO_MA;
        //        _model.GV_CAUTAOSUACHUA = (model.GV_CAUTAOSUACHUA == false ? "0" : "1");
        //        _model.GV_DAODUCLAIXE = (model.GV_DAODUCLAIXE == false ? "0" : "1");
        //        _model.GV_NGHIEPVUVANTAI = (model.GV_NGHIEPVUVANTAI == false ? "0" : "1");
        //        _model.GV_LUATGTDB = (model.GV_LUATGTDB == false ? "0" : "1");
        //        _model.GV_KYTHUATLAIXE = (model.GV_KYTHUATLAIXE == false ? "0" : "1");
        //        _model.GV_MOTA = model.GV_MOTA;
        //        _model.GV_ACTIVE = (model.GV_ACTIVE == false ? "0" : "1");
        //        _model.GV_NGUOISUA = User.Identity.Name;
        //        _model.GV_NGAYSUA = DateTime.Now;

        //        if (model.vGV_ANHCD != null)
        //        {
        //            HttpPostedFileBase file = model.vGV_ANHCD;
        //            using (var binaryReader = new BinaryReader(file.InputStream))
        //            {
        //                _model.GV_ANHCD = binaryReader.ReadBytes(file.ContentLength);
        //            }
        //        }


        //        _db.Update<DGV>(_model);
        //        return RedirectToAction("Index");
        //    }

        //    return View(model);
        //}
        //public void initialCategoryEditAction(CrudModelDGV view)
        //{
        //    // ApplicationUser curUser = getCurrentUser();
        //    ViewBag.GV_DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");
        //    ViewBag.GV_HANGDT_MAs = new SelectList(_db.GetAll<SHANGDT>(), "HANGDT_MA", "HANGDT_TEN");
        //    ViewBag.GV_HGPLX_MAs = new SelectList(_db.GetAll<SHANGGPLX>(), "HGPLX_MA", "HGPLX_TEN");
        //    ViewBag.GV_NOICT_DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");
        //    ViewBag.GV_NOICT_DVHC_MAs = new SelectList(_db.GetAll<SDONVIHC>(), "DVHC_MA", "DVHC_TENDAYDU");
        //    ViewBag.GV_HTDTAO_MAs = new SelectList(_db.Filter<SHTDAOTAO>(o => o.HTDT_ACTIVE == "1"), "HTDT_MA", "HTDT_MOTA");
        //    ViewBag.GV_HTTUYENDUNGs = "CT.HĐ";
        //}

        //public string GetkeyIdentity_GV_MA(string gv_DVGTVT_MA)
        //{
        //    var maxGV_MA = _db.Filter<DGV>(o => o.GV_DVGTVT_MA == gv_DVGTVT_MA).OrderByDescending(o => o.GV_MA).FirstOrDefault();
        //    string strGV_MA = gv_DVGTVT_MA + "001";

        //    if (maxGV_MA != null)
        //    {
        //        var curGV_MA = int.Parse(maxGV_MA.GV_MA) + 1;
        //        strGV_MA = curGV_MA.ToString();
        //    }
        //    return strGV_MA;
        //}

        #endregion
        #region Detele
        //public ActionResult Delete(decimal id)
        //{
        //    CrudModelDGV _model = new CrudModelDGV();
        //    DGV model = _db.GetOne<DGV>(o => o.ID == id);
        //    _db.DeleteItem<DGV>(model);
        //    return RedirectToAction("Index");
        //}
        #endregion
        #endregion
    }
}